package cn.iocoder.yudao.module.signature.service.developeraccountversion;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccountversion.vo.DeveloperAccountVersionPageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccountversion.vo.DeveloperAccountVersionRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.developeraccountversion.vo.DeveloperAccountVersionSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.developeraccountversion.DeveloperAccountVersionDO;
import cn.iocoder.yudao.module.signature.dal.mysql.developeraccountversion.DeveloperAccountVersionMapper;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.signature.enums.ErrorCodeConstants.DEVELOPER_ACCOUNT_VERSION_NOT_EXISTS;

/**
 * 账号版本信息 Service 实现类
 *
 * @author base
 */
@Service
@Validated
public class DeveloperAccountVersionServiceImpl implements DeveloperAccountVersionService {

    @Resource
    private DeveloperAccountVersionMapper developerAccountVersionMapper;

    @Override
    public Long createDeveloperAccountVersion(DeveloperAccountVersionSaveReqVO createReqVO) {
        // 插入
        DeveloperAccountVersionDO developerAccountVersion = BeanUtils.toBean(createReqVO, DeveloperAccountVersionDO.class);
        developerAccountVersionMapper.insert(developerAccountVersion);
        // 返回
        return developerAccountVersion.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createBatch(List<DeveloperAccountVersionSaveReqVO> reqVOS) {
        if(CollectionUtil.isEmpty(reqVOS)){
            return;
        }
        List<DeveloperAccountVersionDO> accountVersionDOS = BeanUtils.toBean(reqVOS, DeveloperAccountVersionDO.class);
        accountVersionDOS.stream().forEach(developerAccountVersionDO -> developerAccountVersionDO.setId(null));
        developerAccountVersionMapper.insertBatch(accountVersionDOS);
    }

    @Override
    public void updateDeveloperAccountVersion(DeveloperAccountVersionSaveReqVO updateReqVO) {
        // 校验存在
        validateDeveloperAccountVersionExists(updateReqVO.getId());
        // 更新
        DeveloperAccountVersionDO updateObj = BeanUtils.toBean(updateReqVO, DeveloperAccountVersionDO.class);
        developerAccountVersionMapper.updateById(updateObj);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByAccountIdAndTypeCode(Long accountId, String versionTypeCode, Integer enableFlag) {
        DeveloperAccountVersionDO developerAccountVersionDO = developerAccountVersionMapper.selectByAccountIdAndTypeCode(accountId, versionTypeCode);
        if (developerAccountVersionDO == null) {
            DeveloperAccountVersionDO insertDO = new DeveloperAccountVersionDO();
            insertDO.setAccountId(accountId);
            insertDO.setVersionTypeCode(versionTypeCode);
            insertDO.setEnableFlag(enableFlag);
            developerAccountVersionMapper.insert(insertDO);
            return;
        }
        developerAccountVersionDO.setEnableFlag(enableFlag);
        developerAccountVersionMapper.updateById(developerAccountVersionDO);
    }

    @Override
    public Map<Long, List<DeveloperAccountVersionRespVO>> getByAccounts(List<Long> accountIds) {
        if(CollectionUtil.isEmpty(accountIds)){
            return Maps.newHashMap();
        }
        List<DeveloperAccountVersionDO> accountVersionDOS = developerAccountVersionMapper.selectByAccountIds(accountIds);
        if(CollectionUtil.isEmpty(accountVersionDOS)){
            return Maps.newHashMap();
        }

        List<DeveloperAccountVersionRespVO> respVOS = BeanUtils.toBean(accountVersionDOS, DeveloperAccountVersionRespVO.class);

        return respVOS.stream().collect(Collectors.groupingBy(DeveloperAccountVersionRespVO::getAccountId));
    }

    @Override
    public void deleteDeveloperAccountVersion(Long id) {
        // 校验存在
        validateDeveloperAccountVersionExists(id);
        // 删除
        developerAccountVersionMapper.deleteById(id);
    }

    private void validateDeveloperAccountVersionExists(Long id) {
        if (developerAccountVersionMapper.selectById(id) == null) {
            throw exception(DEVELOPER_ACCOUNT_VERSION_NOT_EXISTS);
        }
    }

    @Override
    public DeveloperAccountVersionDO getDeveloperAccountVersion(Long id) {
        return developerAccountVersionMapper.selectById(id);
    }

    @Override
    public PageResult<DeveloperAccountVersionDO> getDeveloperAccountVersionPage(DeveloperAccountVersionPageReqVO pageReqVO) {
        return developerAccountVersionMapper.selectPage(pageReqVO);
    }

}